<template>
  <div>
    <div style="text-align: left; padding: 5px 5px 0px 5px">
    <el-input style="width: 200px; padding: 0px 5px 0px 0px" placeholder="请输入商品条码" v-model="barcode" ></el-input>
    <el-button
      size="mini"
      @click="handFind()"
      type="primary">查询
    </el-button>
    <el-button
      size="mini"
      @click="handleCreate()"
      type="info">添加
    </el-button>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="ID"
        width="100">
      </el-table-column>
      <el-table-column
        prop="name"
        label="商品名称"
        width="100">
      </el-table-column>
      <el-table-column
        prop="barcode"
        label="商品条码"
        width="100">
      </el-table-column>
      <el-table-column
        prop="price"
        label="商品价格"
        width="100">
      </el-table-column>
      <el-table-column
        prop="type"
        label="商品类型"
        width="100">
        <template slot-scope="scope">
          {{ scope.row.type === "0" ? '计件商品' : '计重商品' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="unit"
        label="单位">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="pageable.pageNumber"
        :page-sizes="[10, 20, 40]"
        :page-size="pageable.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalElements">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import itemService from '../global/service/itemService';

export default {
  name: 'ItemList',
  data() {
    return {
      tableData: [],
      pageable: {
        pageNumber: 1,
        pageSize: 10
      },
      totalElements: 0,
      barcode: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleEdit(index, row) {
      let id = row.id;
      this.$router.push({
        name: 'ItemUpdate',
        query: {
          id: id,
        },
      })
    },
    async getList() {
      let res = await itemService.getList(
        {
          barcode: this.barcode,
          page: this.pageable.pageNumber - 1,
          size: this.pageable.pageSize,
        }
      );
      this.tableData = res.content
      this.totalElements = res.totalElements

    },
    handleDelete(index, row) {
      itemService.delete(row.id).then(() => {
        this.getList();
      }).finally(() => {
      })
    },
    handleCreate() {
      this.$router.push('/item/create')
    },
    handFind() {
      this.getList()
    },
    handleSizeChange(val) {
      this.pageable.pageSize = val;
      this.getList()
    },
    handleCurrentChange(val) {
      this.$route.query.page = val;
      this.getList()
    },
  }

}
</script>
